import { Button, Image, message, Space } from 'ant-design-vue'
import moment from 'moment'
import { defineComponent, ref } from 'vue'

import { jindouLifeApi } from '@/apis'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { useDialog } from '@/hooks'
import { bus, optionsToMap } from '@/utils'
import { handlePrice } from '@/utils/bsshow'
import { couponTypeOptions, refundStatusOptions } from '@/views/order/constants'

export default defineComponent({
  name: 'JdlOrderManage',
  setup() {
    const { showConfirm } = useDialog()
    const orderStatusOptions = [
      { label: '处理中', value: -1 },
      { label: '待支付', value: 1 },
      { label: '已支付', value: 2 },
      { label: '已作废', value: 3 },
      { label: '已完成', value: 4 },
      { label: '已退款', value: 5 },
      { label: '已取消', value: 6 }
    ]

    const thirdOrderStatusOptions = [
      { label: '处理中', value: -1 },
      { label: '待支付', value: 0 },
      { label: '已支付', value: 1 },
      { label: '核销', value: 3 },
      { label: '已退款', value: 5 }
    ]

    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        formConfig: {
          items: [
            {
              title: '站点名称',
              field: 'city',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              title: '商品名称',
              field: 'productTitle',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              title: '订单编号',
              field: 'orderNo',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              title: '手机号',
              field: 'customerPhoneNumber',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入',
                  allowClear: true
                }
              }
            },
            {
              title: '订单状态',
              field: 'orderStatus',
              itemRender: {
                name: TABLE_ITEM_RENDER.ASelect,
                props: {
                  placeholder: '全部',
                  allowClear: true
                },
                options: orderStatusOptions
              }
            },
            {
              title: '支付时间',
              itemRender: {
                name: TABLE_RENDERER.FormItemDate,
                props: {
                  type: 'range',
                  placeholder: ['开始时间', '结束时间'],
                  valueFormat: 'x',
                  showTime: {
                    defaultValue: [
                      moment('00:00:00', 'HH:mm:ss'),
                      moment('23:59:59', 'HH:mm:ss')
                    ]
                  },
                  keys: ['startTime', 'endTime']
                }
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columnConfig: {
          minWidth: 100,
          resizable: true
        },
        columns: [
          {
            title: '产品ID',
            field: 'productId'
          },
          {
            title: '站点名称',
            field: 'city'
          },
          {
            title: '订单号',
            field: 'orderNo',
            minWidth: 220
          },
          {
            title: '第三方订单号',
            field: 'channelOrderId',
            minWidth: 220
          },
          {
            title: '产品',
            width: 250,
            slots: {
              default: 'goodsInfo'
            }
          },
          {
            title: '套餐',
            field: 'subTitle',
            width: 150
          },
          {
            title: '状态',
            field: 'orderStatusView',
            formatter: ({ cellValue }) => {
              return optionsToMap(orderStatusOptions)[cellValue]
            }
          },
          {
            title: '退款方式',
            field: 'refundType',
            minWidth: 100,
            formatter: ({ cellValue }) => {
              return optionsToMap(refundStatusOptions)[cellValue]
            }
          },
          {
            title: '第三方状态',
            field: 'orderStatusViewThree',
            formatter: ({ cellValue }) => {
              return optionsToMap(thirdOrderStatusOptions)[cellValue]
            }
          },
          {
            title: '单价',
            width: 130,
            slots: {
              default: 'salePrice'
            }
          },
          {
            title: '数量',
            field: 'quantity'
          },
          {
            title: '优惠券',
            minWidth: 100,
            slots: {
              default: 'couponInfo'
            }
          },
          {
            title: '实付金额',
            width: 130,
            slots: {
              default: 'salePrice'
            }
          },
          {
            title: '下单人',
            field: 'nickname'
          },
          {
            title: '下单人手机号',
            field: 'mobile'
          },
          {
            title: '客户姓名',
            field: 'customerName'
          },
          {
            title: '客户手机号',
            field: 'customerPhoneNumber'
          },

          {
            title: '下单时间',
            field: 'createTime',
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss'],
            minWidth: 150
          },
          {
            title: '支付时间',
            field: 'payTime',
            formatter: [TABLE_FORMAT.Date, 'yyyy-MM-dd HH:mm:ss'],
            minWidth: 150
          },
          {
            title: '支付微信流水号',
            field: 'wxPayNo',
            minWidth: 230
          },
          {
            title: '退款微信流水号',
            field: 'refundPayNo',
            minWidth: 230
          },
          {
            title: '备注',
            field: 'orderDesc',
            minWidth: 230
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            },
            width: 80
          }
        ],
        pagerConfig: {
          pageSize: 15
        },
        proxyConfig: {
          seq: true,
          form: true,
          props: {
            result: 'dataList',
            total: 'page.totalCount'
          },
          ajax: {
            query: async ({ page, form }) => {
              const data = await jindouLifeApi.getOrderList({
                pageParameter: {
                  pageSize: page.pageSize,
                  currentPage: page.currentPage
                },

                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    //售后
    function handleAfterSales(row) {
      showConfirm({
        content: '是否确认售后，发起后将原路退款给到用户并生成新的售后订单？',
        onOk: async () => {
          try {
            await jindouLifeApi.afterSale(row.orderNo)
            tableRef.value.refresh()
            message.success('操作完成')
          } catch (error) {}
        }
      })
    }

    return {
      tableRef,
      tableConfig,
      handleAfterSales
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            goodsInfo: ({ row }) => {
              return (
                <Space>
                  <Image src={row.faceImg} width="80px"></Image>
                  <p>{row.productTitle}</p>
                </Space>
              )
            },
            couponInfo: ({ row }) => {
              if (row.couponType) {
                return (
                  <Space>
                    {`${
                      optionsToMap(couponTypeOptions)[row.coupon.couponType]
                    } ${row.coupon.discount}折`}
                  </Space>
                )
              }
              return '无'
            },
            goodsPrice: ({ row }) => {
              return (
                <Space>{handlePrice(row.settlePrice, row.beanPrice)}</Space>
              )
            },
            salePrice: ({ row }) => {
              return (
                <Space>
                  {handlePrice(row.settlePricePaid, row.beanPricePaid)}
                </Space>
              )
            },
            operation: ({ row }) => (
              <Space>
                <Button
                  disabled={row.refundStatus != 1}
                  type="link"
                  size="small"
                  onClick={() => this.handleAfterSales(row)}
                >
                  售后
                </Button>
              </Space>
            )
          }}
          {...this.tableConfig}
        />
      </div>
    )
  }
})
